//
//@File    :	14.2_student.cpp
//@Time    :	2022/01/19 20:03:31
//@Auther  :	Sun Sheng
//@Contact :	970340843@qq.com
//@Desc    :	Student class using containment
//

#include "14.1_studentc.h"
using std::endl;
using std::istream;
using std::ostream;
using std::string;

//public methods
double Student::Average() const
{
    if (scores.size() > 0)
    {
        return scores.sum() / scores.size();
    }
    else
    {
        return 0;
    }
}
const string &Student::Name() const
{
    return name;
}

double &Student::operator[](int i)
{
    return scores[i];
    //use valarray<double>::operator[]()
}

double Student::operator[](int i) const
{
    return scores[i];
}

//private methods
ostream &Student::arr_out(ostream &os) const
{
    int i;
    int lim = scores.size();
    if (lim > 0)
    {
        for (int i = 0; i < lim; i++)
        {
            os << scores[i] << " ";
            if (i % 5 == 4)
            {
                os << endl;
            }
        }
        if (i % 5 != 0)
        {
            os << endl;
        }
    }
    else
    {
        os << " empty array ";
    }
    return os;
}

//friends
//use string version od operator>>()
istream &operator>>(istream &is, Student &stu)
{
    is >> stu.name;
    return is;
}

//use string friend getline(ostream &,const string &)
istream &getline(istream &is, Student &stu)
{
    getline(is, stu.name);
    return is;
}

//use string version of operator<<()
ostream &operator<<(ostream &os, const Student &stu)
{
    os << "Scores for " << stu.name << ":\n";
    stu.arr_out(os); //use private methods for scores
    return os;
}